package com.ichir.projects.uiflow.internal.resolvers;

import java.util.HashMap;
import java.util.Map;

import com.ichir.projects.uiflow.definition.FlowType;
import com.ichir.projects.uiflow.definition.IFlowDefinition;
import com.ichir.projects.uiflow.definition.IFlowPart;
import com.ichir.projects.uiflow.internal.api.IFlowResolver;
import com.ichir.projects.uiflow.ui.api.IUiAction;
import com.ichir.projects.uiflow.ui.api.IUiPart;

/**
 * {@link IFlowResolver} default implementation.
 * 
 * @author mahieddine_ichir
 */
public class FlowResolver implements IFlowResolver {

	private static Map<FlowType, IFlowResolver> resolvers;

	static {
		resolvers = new HashMap<FlowType, IFlowResolver>();

		resolvers.put(FlowType.VIEW, new FlowViewResolver());
		resolvers.put(FlowType.ACTION, new FlowActionResolver());
	}

	@Override
	public IFlowPart resolveFlowPart(IFlowDefinition flowDefinition, IUiPart uiPart) {
		System.out.println(">> get flow part for UIPart "+uiPart.getClass());
		if (uiPart instanceof IUiAction) {
			return resolvers.get(FlowType.ACTION).resolveFlowPart(flowDefinition, uiPart);
		} else {
			return resolvers.get(FlowType.VIEW).resolveFlowPart(flowDefinition, uiPart);
		}
	}

	@Override
	public IFlowPart getFlowPartById(IFlowDefinition flowDefinition, String flowPartId) {
		IFlowPart flowPart = resolvers.get(FlowType.ACTION).getFlowPartById(flowDefinition, flowPartId);
		if (flowPart == null) {
			flowPart = resolvers.get(FlowType.VIEW).getFlowPartById(flowDefinition, flowPartId);
		}
		return flowPart;
	}
}
